package com.example.outdoor;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class updateProduct extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_update_product);
		//pbactivityUpdateAvenue = (ProgressBar) findViewById(R.id.pbactivityUpdateAvenue);
		//pbactivityUpdateAvenue.setVisibility(View.GONE);
		// Read var from Intent
		Intent intent= getIntent();
		final String ID = intent.getStringExtra("ID");
		
		// Show Data
		ShowData(ID);
		
		// btnSave (Save)
        final Button save = (Button) findViewById(R.id.btnProductSave);
        save.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {  
            	//pbactivityUpdateAvenue.setVisibility(View.VISIBLE);
            	// If Save Complete
            	if(UpdateData(ID))
            	{
                	// Open Form ListUpdate
                	Intent newActivity = new Intent(updateProduct.this,ListOfProduct.class);
                	startActivity(newActivity);            		
            	}
            }
        });		
		
		// btnCancel (Cancel)
        final Button cancel = (Button) findViewById(R.id.btnProductCancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {   
            	//pbactivityUpdateAvenue.setVisibility(View.VISIBLE);
            	// Open Form ListUpdate
            	Intent newActivity = new Intent(updateProduct.this,ListOfProduct.class);
            	startActivity(newActivity);
            }
        });
        
	}
	
	public void ShowData(String MemID)
	{
		// txtMemberID, txtName, txtTel
		final TextView tMemberID = (TextView) findViewById(R.id.txtProductID);
		final EditText tName = (EditText) findViewById(R.id.txtBrandcode);
		final EditText txtAvenueDesc=(EditText)findViewById(R.id.txtBranddescription);
					
		// new Class DB
		final myDBForProduct myDb = new myDBForProduct(this);
		
		// Show Data
		String arrData[] = myDb.SelectData(MemID);
    	if(arrData != null)
    	{
    		tMemberID.setText(arrData[0]);
    		tName.setText(arrData[1]);
    		txtAvenueDesc.setText(arrData[2]);
    	}
   
	}
	
	public boolean UpdateData(String MemID)
	{
		
		// txtName, txtTel
		final EditText tName = (EditText) findViewById(R.id.txtBrandcode);
		final EditText txtAvenueDesc=(EditText)findViewById(R.id.txtBranddescription);
				
		// Dialog
		final AlertDialog.Builder adb = new AlertDialog.Builder(this);
		AlertDialog ad = adb.create();
				
		// Check Name
		if(tName.getText().length() == 0)
		{
            ad.setMessage("Please input [Name] ");
            ad.show();
            tName.requestFocus();
            return false;
		}	
		
		
				
		
		// new Class DB
		final myDBForProduct myDb = new myDBForProduct(this);
    		
    	// Save Data
    	long saveStatus = myDb.UpdateData(MemID,
    			tName.getText().toString(),txtAvenueDesc.getText().toString());
    	if(saveStatus <=  0)
    	{
            ad.setMessage("Error!! ");
            ad.show();
            return false;
    	}
    	
		 Toast.makeText(updateProduct.this,"Update Data Successfully. ",
	 		     Toast.LENGTH_SHORT).show();   
		
		return true;
		
	}
}
